/* Hawk - A Component Based Test Framework
 *
 * Copyright (C) 2010 http://code.google.com/p/hawk-framework/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.hawk.applications.finder.swing.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;

import org.hawk.applications.finder.swing.communication.ThreadClient;
import org.hawk.applications.finder.swing.communication.ThreadServer;

/**
 * User interface to search the recovery information about other person
 * 
 * @author Rodrigo Prestes Machado
 */
@SuppressWarnings("serial")
public class TabSearch extends JComponent {
	
	/* Attributes */
	private JButton buttonStartMonitor;
	private JButton buttonStopMonitor;
	private JButton buttonClient;
	private ThreadServer ts;
	
	public TabSearch(){
		
		// creating layout
		setLayout(new FlowLayout());
		
		// creating gui components
		buttonStartMonitor = new JButton("Start Server");
		buttonStartMonitor.addActionListener(new EventStartServer());
		
		buttonStopMonitor = new JButton("Stop Server");
		buttonStopMonitor.addActionListener(new EventStopServer());
		buttonStopMonitor.setVisible(false);
		
		buttonClient = new JButton("Start Client");
		buttonClient.addActionListener(new EventClient());
		
		// adding gui components on screen
		add(buttonStartMonitor);
		add(buttonStopMonitor);
		add(buttonClient);
	}
	
	/**
	 * Inner class to handle events from the start button
	 */
	private class EventStartServer implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
		
			// Hiding the start button and shows the stop button
			buttonStartMonitor.setVisible(false);
			buttonStopMonitor.setVisible(true);
			
			// starting the search thread
			ts = new ThreadServer();
			ts.start();
		}
	}
	
	/**
	 * Inner class to handle events from the stop button
	 */
	private class EventStopServer implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
		
			// Hide stop button and shows the start button
			buttonStartMonitor.setVisible(true);
			buttonStopMonitor.setVisible(false);
			
			// stops the thread
			ts.interrupt();
		}
	}
	
	/**
	 * Inner class to handle events from the client button
	 */
	private class EventClient implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// starting the client class
			ThreadClient tc = new ThreadClient();
			tc.start();
		}
	}
}